<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="运营模式">
        <el-select v-model="queryParams.modelName" placeholder="请选择运营模式" clearable>
          <el-option v-for="item in operateModel" :key="item.index" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="营业分组">
        <el-select v-model="queryParams.groupName" placeholder="请选择营业分组" clearable>
          <el-option v-for="item in operateGroup" :key="item.id" :label="item.groupName"
            :value="item.groupName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备状态">
        <el-select v-model="queryParams.status" placeholder="请选择设备状态" clearable>
          <el-option label="正常" value="1"></el-option>
          <el-option label="停用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备编号">
        <el-input v-model="queryParams.code" placeholder="请输入设备编号" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableData" border>
      <el-table-column label="设备编号" align="center" prop="code" />
      <el-table-column label="运营模式" align="center" prop="modelName" />
      <el-table-column label="营业分组" align="center" prop="groupName" />
      <el-table-column label="消费总金额" align="center" prop="sum" />
      <el-table-column label="消费总次数" align="center" prop="count" />
      <el-table-column label="设备状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" plain v-show="(scope.row.status == 1)" disabled>正常</el-button>
          <el-button size="mini" type="primary" plain v-show="(scope.row.status == 0)" disabled>禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column label="最后工作时间" align="center" prop="time" />
    </el-table>

    <!-- <pagination v-show="total > 0" :total="total" page.sync="1" limit.sync="5" @pagination="getList" /> -->

    <!-- <el-pagination class="pagination" background layout="prev, pager, next" :total="total" :page-size="5"
      @current-change="handleCurrentChange"></el-pagination> -->

  </div>
</template>

<script>
import { equipList, operateList, groupList } from "@/api/equipmentCenter/index.js";

export default {
  name: "Notice",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      tableData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        code: "",
        groupName: "",
        modelName: "",
        status: "",
      },
      //运营模式下拉框
      operateModel: [],
      //营业分组下拉框
      operateGroup: []
    };
  },
  created() {
    this.getList();
    this.operateList();
    this.groupList()
  },
  methods: {
    handleCurrentChange(e) {
      //当前页
      // console.log(e-1)
      // this.form.current = e - 1
      this.getList()
    },
    //获取运营模式下拉框
    operateList() {
      operateList().then(res => {
        // console.log(res);
        this.operateModel = res.data.data
      })
    },
    //获取营业分组下拉框
    groupList() {
      groupList().then(res => {
        // console.log(res);
        this.operateGroup = res.data.data
      })
    },
    /** 查询列表 */
    getList() {
      // if (this.queryParams.status == "1") {
      //   this.queryParams.status = 1
      // }
      // this.loading = true;
      equipList(this.queryParams).then(res => {
        console.log(res);
        if (res.data.success == true) {
          this.tableData = res.data.data.list;
          this.total = res.data.data.total;
          // this.loading = false;
        } else {
          this.$message.info(res.data.message)
        }
      }).catch(error => {
        console.log(error.response.status);
        if (error.response.status == 401) {
          this.$confirm('登录状态过期, 请重新登录!', '提示', {
            confirmButtonText: '确定',
            // cancelButtonText: '取消',
            showCancelButton: false,
            type: 'warning'
          }).then(() => {
            this.$router.push('/login')
          }).catch(() => {

          })
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noticeId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        code: "",
        groupName: "",
        modelName: "",
        status: "",
      }
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.noticeId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公告";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const noticeId = row.noticeId || this.ids
      getNotice(noticeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公告";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.noticeId != undefined) {
            updateNotice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNotice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.noticeId || this.ids
      this.$modal.confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？').then(function () {
        return delNotice(noticeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    }
  }
};
</script>
